﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;

/// <summary>
///clsDrawImage 的摘要说明
////****************************************************** 
//FileName:文件名称
//Description: 该类内容为绘制图形
//Copyright (c) : 通力凯顿（北京）系统集成有限公司
//Writer:陆迪
//create Date:2012-12-28 16:48
//******************************************************/

/// 
/// </summary>
/// 
namespace uMES.LeanManufacturing.Common
{
    public class clsDrawImage
    {
        public clsDrawImage()
        {

        }

        //绘制上下标图形
        public Bitmap DrawTolerance(String strMid, String strUp, String strDown)
        {
            Font mFont = new Font("宋体", 12);   //绘制文本字体
            Font mFont2 = new Font("宋体", 8);   //绘制上下标文本字体

            Graphics g = Graphics.FromImage(new Bitmap(1, 1));

            int intLength, intMidLength, intUpLength, intDownLength, intMaxLength, intStart;
            float fXPoint, fYPoint;

            fXPoint = 0;
            fYPoint = 0;
            intMidLength = Convert.ToInt32(g.MeasureString(strMid, mFont).Width);
            intUpLength = Convert.ToInt32(g.MeasureString(strUp, mFont2).Width);
            intDownLength = Convert.ToInt32(g.MeasureString(strDown, mFont2).Width);

            if (intUpLength >= intDownLength)
            {
                intMaxLength = intUpLength;
            }
            else
            {
                intMaxLength = intDownLength;
            }
            intLength = intMidLength + intMaxLength;
            intStart = intMidLength;

            //创建位图并获取绘图设备
            Bitmap objBmp = new Bitmap(intLength, 18);
            Graphics objGraph = Graphics.FromImage(objBmp);

            //背景默认为黑色，需要用白色替换一下
            objGraph.Clear(Color.White);

            //创建绘图用的画笔和画刷
            Pen objPen = new Pen(Color.Black);
            Brush FillBrush = new SolidBrush(Color.SandyBrown); //填充色
            Brush TextBrush = Brushes.Black;   //文本颜色

            objGraph.DrawString(strMid, mFont, TextBrush, fXPoint, fYPoint);

            if (strUp != "")
            {
                objGraph.DrawString(strUp, mFont2, TextBrush, fXPoint + intStart, fYPoint);
            }
            if (strDown != "")
            {
                objGraph.DrawString(strDown, mFont2, TextBrush, fXPoint + intStart, fYPoint + 8);
            }

            ////保存
            return objBmp;
        }

        //绘制粗糙度图形
        public Bitmap DrawRoughness(String strLeft, String strRight)
        {
            int intFontSize, intFontSize2;
            intFontSize = 9;
            intFontSize2 = 5;
            Font mFont = new Font("宋体", 8);   //绘制文本字体
            Font mFont2 = new Font("宋体", 10);   //绘制右侧上下文本字体

            int intLength, intLeftLength, intRightLength;
            float fXPoint, fYPoint;

            fXPoint = 0;
            fYPoint = 0;

            intLeftLength = strLeft.Length;
            //strRight = "<" + strUp + "," + strDown + ">";
            intRightLength = strRight.Length;
            Point point1 = new Point(16, 10);
            Point point2 = new Point(0, 10);
            Point point3 = new Point(8, 20);
            Point point4 = new Point(24, 0);
            Point point5 = new Point(31, 0);


            if (strLeft != "")
            {
                if (strRight != "")
                {
                    //指定加工方法时
                    intLength = 3 * intFontSize2 + (intRightLength) * intFontSize;

                    //创建位图并获取绘图设备
                    Bitmap objBmp = new Bitmap(intLength, 22);
                    Graphics objGraph = Graphics.FromImage(objBmp);

                    //背景默认为黑色，需要用白色替换一下
                    objGraph.Clear(Color.White);

                    //创建绘图用的画笔和画刷
                    Pen objPen = new Pen(Color.Black);
                    Brush FillBrush = new SolidBrush(Color.SandyBrown); //填充色
                    Brush TextBrush = Brushes.Black;   //文本颜色

                    objGraph.DrawString(strLeft, mFont, TextBrush, fXPoint, fYPoint);
                    Point[] PointLine = { point1, point2, point3, point4, point5 };
                    objGraph.DrawLines(objPen, PointLine);
                    objGraph.DrawString(strRight, mFont2, TextBrush, 13, 10);

                    return objBmp;


                }
                else
                {
                    //去除材料时
                    intLength = 25;

                    //创建位图并获取绘图设备
                    Bitmap objBmp = new Bitmap(intLength, 22);
                    Graphics objGraph = Graphics.FromImage(objBmp);

                    //背景默认为黑色，需要用白色替换一下
                    objGraph.Clear(Color.White);

                    //创建绘图用的画笔和画刷
                    Pen objPen = new Pen(Color.Black);
                    Brush FillBrush = new SolidBrush(Color.SandyBrown); //填充色
                    Brush TextBrush = Brushes.Black;   //文本颜色

                    objGraph.DrawString(strLeft, mFont, TextBrush, fXPoint, fYPoint);
                    Point[] PointLine = { point1, point2, point3, point4 };
                    objGraph.DrawLines(objPen, PointLine);

                    return objBmp;
                }
            }
            else
            {
                //不去除材料时
                intLength = 25;

                //创建位图并获取绘图设备
                Bitmap objBmp = new Bitmap(intLength, 22);
                Graphics objGraph = Graphics.FromImage(objBmp);

                //背景默认为黑色，需要用白色替换一下
                objGraph.Clear(Color.White);

                //创建绘图用的画笔和画刷
                Pen objPen = new Pen(Color.Black);
                Brush FillBrush = new SolidBrush(Color.SandyBrown); //填充色
                Brush TextBrush = Brushes.Black;   //文本颜色

                //objGraph.DrawString(strLeft, mFont, TextBrush, fXPoint, fYPoint);
                objGraph.DrawEllipse(objPen, 4, 10, 8, 8);

                Point[] PointLine = { point2, point3, point4 };
                objGraph.DrawLines(objPen, PointLine);

                return objBmp;
            }

        }

     
        //绘制普通文本图形
        public Bitmap DrawString(String strInput)
        {
            int intFontSize;
            intFontSize = 10;

            Font mFont = new Font("宋体", 10);   //绘制文本字体

            int intLength, intMidLength, intMaxLength, intStart;
            float fXPoint, fYPoint;

            fXPoint = 0;
            fYPoint = 0;
            strInput = strInput.Replace("&nbsp", " ");
            intMidLength = System.Text.Encoding.Default.GetBytes(strInput).Length; //strInput.Length;

            intLength = intMidLength * intFontSize;

            intStart = (intMidLength) * (intFontSize - 1) + 12;
            intLength = intLength + 5;



            //创建位图并获取绘图设备
            Bitmap objBmp = new Bitmap(intLength, 18);
            Graphics objGraph = Graphics.FromImage(objBmp);

            //背景默认为黑色，需要用白色替换一下
            objGraph.Clear(Color.White);

            //创建绘图用的画笔和画刷
            Pen objPen = new Pen(Color.Black);
            Brush FillBrush = new SolidBrush(Color.SandyBrown); //填充色
            Brush TextBrush = Brushes.Black;   //文本颜色

            objGraph.DrawString(strInput, mFont, TextBrush, fXPoint, fYPoint);

            ////保存
            ////objBmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
            //String strFileDoc, strFilePath, strFileName;
            //strFileDoc = @"C:\Ludi\VS2008\ToleranceTest\ToleranceTest\Images\";
            ////intImageIndex = (int)Session["intMaxImageIndex"] + 1;
            ////Session["intMaxImageIndex"] = intImageIndex;
            //strFileName = "ImageTem-" + intMaxImageIndex + ".png";
            //strFilePath = strFileDoc + strFileName;
            //objBmp.Save(strFilePath);
            //return @"<img src='../Images/" + strFileName + "'>";
            return objBmp;
        }

        //在原有图形上增加新图形
        public Bitmap DrawImageAdd(System.Drawing.Image ImgOld, System.Drawing.Image ImgAdd)
        {
            int intFontSize;
            intFontSize = 9;

            Font mFont = new Font("宋体", 12);   //绘制文本字体

            int intLength, intMidLength, intMaxLength, intStart;
            float fXPoint, fYPoint;

            fXPoint = 0;
            fYPoint = 0;

            int intImgOldWidth, intImgAddWidth;
            intImgOldWidth = ImgOld.Width;
            intImgAddWidth = ImgAdd.Width;
            intLength = intImgOldWidth + intImgAddWidth + 1;
            //创建位图并获取绘图设备
            Bitmap objBmp = new Bitmap(intLength, 22);
            Graphics objGraph = Graphics.FromImage(objBmp);

            //背景默认为黑色，需要用白色替换一下
            objGraph.Clear(Color.White);

            //创建绘图用的画笔和画刷
            Pen objPen = new Pen(Color.Black);
            Brush FillBrush = new SolidBrush(Color.SandyBrown); //填充色
            Brush TextBrush = Brushes.Black;   //文本颜色

            objGraph.DrawImage(ImgOld, 1, 1, intImgOldWidth, 19);
            objGraph.DrawImage(ImgAdd, intImgOldWidth + 1, 1, intImgAddWidth, 19);

            //objGraph.DrawString(strInput, mFont, TextBrush, fXPoint, fYPoint);

            ////保存
            ////objBmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
            //String strFileDoc, strFilePath, strFileName;
            //strFileDoc = @"C:\Ludi\VS2008\ToleranceTest\ToleranceTest\Images\";
            ////intImageIndex = (int)Session["intMaxImageIndex"] + 1;
            ////Session["intMaxImageIndex"] = intImageIndex;
            //strFileName = "ImageTem-" + intMaxImageIndex + ".png";
            //strFilePath = strFileDoc + strFileName;
            //objBmp.Save(strFilePath);
            //return @"<img src='../Images/" + strFileName + "'>";
            return objBmp;
        }


    }
}